﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace MengLin.Studying.PrismApp.Models
{
    public class Protocol_SIS_Model : INotifyPropertyChanged
    {
        private string _sensorMode;
        private double _measurementVolume;
        private double _syringeFlowRate;
        private double _sizeThreshold;
        private double _sampleRunTime;
        private bool _isPreserveSample;
        private bool _isLowVolume;
        private bool _isTare;
        private double _tareVolume;
        private bool _isFlush;
        private string _backgroundMode;
        private double _backgroundMeasurement;
        private string _backgroundMeasurementUnit;
        private bool _isMixing;
        private string _mixingMode;
        private int _mixingCycles;
        private double _mixingVolume;
        private double _mixingFlowRate;
        private bool _isAutoStirrer;
        private double _stirrerSpeed;
        private double _stirrerRunTime;
        private int _replicates;
        private double _airGapVolume;
        private double _delayTime;
        private string _protocolType;
        private string _protocolName;

        public string SensorMode
        {
            get => _sensorMode;
            set
            {
                if (_sensorMode != value)
                {
                    _sensorMode = value;
                    OnPropertyChanged();
                }
            }
        }

        public double MeasurementVolume
        {
            get => _measurementVolume;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Measurement Volume must be a valid number.");
                    return;
                }
                if (_measurementVolume != value)
                {
                    _measurementVolume = value;
                    OnPropertyChanged();
                }
            }
        }

        public double SyringeFlowRate
        {
            get => _syringeFlowRate;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Syringe Flow Rate must be a valid number.");
                    return;
                }
                if (_syringeFlowRate != value)
                {
                    _syringeFlowRate = value;
                    OnPropertyChanged();
                }
            }
        }

        public double SizeThreshold
        {
            get => _sizeThreshold;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Size Threshold must be a valid number.");
                    return;
                }
                if (_sizeThreshold != value)
                {
                    _sizeThreshold = value;
                    OnPropertyChanged();
                }
            }
        }

        public double SampleRunTime
        {
            get => _sampleRunTime;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Sample Run Time must be a valid number.");
                    return;
                }
                if (_sampleRunTime != value)
                {
                    _sampleRunTime = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool IsPreserveSample
        {
            get => _isPreserveSample;
            set
            {
                if (_isPreserveSample != value)
                {
                    _isPreserveSample = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool IsLowVolume
        {
            get => _isLowVolume;
            set
            {
                if (_isLowVolume != value)
                {
                    _isLowVolume = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool IsTare
        {
            get => _isTare;
            set
            {
                if (_isTare != value)
                {
                    _isTare = value;
                    OnPropertyChanged();
                }
            }
        }

        public double TareVolume
        {
            get => _tareVolume;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Tare Volume must be a valid number.");
                    return;
                }
                if (_tareVolume != value)
                {
                    _tareVolume = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool IsFlush
        {
            get => _isFlush;
            set
            {
                if (_isFlush != value)
                {
                    _isFlush = value;
                    OnPropertyChanged();
                }
            }
        }

        public string BackgroundMode
        {
            get => _backgroundMode;
            set
            {
                if (_backgroundMode != value)
                {
                    _backgroundMode = value;
                    OnPropertyChanged();
                }
            }
        }

        public double BackgroundMeasurement
        {
            get => _backgroundMeasurement;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Background Measurement must be a valid number.");
                    return;
                }
                if (_backgroundMeasurement != value)
                {
                    _backgroundMeasurement = value;
                    OnPropertyChanged();
                }
            }
        }

        public string BackgroundMeasurementUnit
        {
            get => _backgroundMeasurementUnit;
            set
            {
                if (_backgroundMeasurementUnit != value)
                {
                    _backgroundMeasurementUnit = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool IsMixing
        {
            get => _isMixing;
            set
            {
                if (_isMixing != value)
                {
                    _isMixing = value;
                    OnPropertyChanged();
                }
            }
        }

        public string MixingMode
        {
            get => _mixingMode;
            set
            {
                if (_mixingMode != value)
                {
                    _mixingMode = value;
                    OnPropertyChanged();
                }
            }
        }

        public int MixingCycles
        {
            get => _mixingCycles;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Mixing Cycles must be a valid number.");
                    return;
                }
                if (_mixingCycles != value)
                {
                    _mixingCycles = value;
                    OnPropertyChanged();
                }
            }
        }

        public double MixingVolume
        {
            get => _mixingVolume;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Mixing Volume must be a valid number.");
                    return;
                }
                if (_mixingVolume != value)
                {
                    _mixingVolume = value;
                    OnPropertyChanged();
                }
            }
        }

        public double MixingFlowRate
        {
            get => _mixingFlowRate;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Mixing Flow Rate must be a valid number.");
                    return;
                }
                if (_mixingFlowRate != value)
                {
                    _mixingFlowRate = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool IsAutoStirrer
        {
            get => _isAutoStirrer;
            set
            {
                if (_isAutoStirrer != value)
                {
                    _isAutoStirrer = value;
                    OnPropertyChanged();
                }
            }
        }

        public double StirrerSpeed
        {
            get => _stirrerSpeed;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Stirrer Speed must be a valid number.");
                    return;
                }
                if (_stirrerSpeed != value)
                {
                    _stirrerSpeed = value;
                    OnPropertyChanged();
                }
            }
        }

        public double StirrerRunTime
        {
            get => _stirrerRunTime;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Stirrer Run Time must be a valid number.");
                    return;
                }
                if (_stirrerRunTime != value)
                {
                    _stirrerRunTime = value;
                    OnPropertyChanged();
                }
            }
        }

        public int Replicates
        {
            get => _replicates;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Replicates must be a valid number.");
                    return;
                }
                if (_replicates != value)
                {
                    _replicates = value;
                    OnPropertyChanged();
                }
            }
        }

        public double AirGapVolume
        {
            get => _airGapVolume;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Air Gap Volume must be a valid number.");
                    return;
                }
                if (_airGapVolume != value)
                {
                    _airGapVolume = value;
                    OnPropertyChanged();
                }
            }
        }

        public double DelayTime
        {
            get => _delayTime;
            set
            {
                if (!IsValidNumber(value))
                {
                    ShowErrorMessage("Delay Time must be a valid number.");
                    return;
                }
                if (_delayTime != value)
                {
                    _delayTime = value;
                    OnPropertyChanged();
                }
            }
        }

        public string ProtocolType
        {
            get => _protocolType;
            set
            {
                if (_protocolType != value)
                {
                    _protocolType = value;
                    OnPropertyChanged();
                }
            }
        }

        public string ProtocolName
        {
            get => _protocolName;
            set
            {
                if (_protocolName != value)
                {
                    _protocolName = value;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private bool IsValidNumber(object value)
        {
            return double.TryParse(value.ToString(), out _);
        }

        private void ShowErrorMessage(string message)
        {
            MessageBox.Show(message, "Input Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}